<?php

class wcNews
{
    //TODO ADD FUNCTION FOR ARCHIVING
    //TODO Add function for taxonomy, so different news categories can be possible
 
    /// get latest news in the system , limit variable control the number of returned news articles, you must also specify the id of the language
	static function getLatest($params=array())
	{
		//PARAMS: idnode + number of news items to show
		/*
			TODO ADD SUPPORT FOR OLDER NEWS PAGE
		*/
		$limit 		=  10;
		$idlanguage	= 'en';
		
		if (isset( $params) )
		{
			if (is_array( $params ))
			{
				if ( isset( $params['limit'] ))
					$limit = $params['limit'];
	
				if ( isset( $params['idlanguage'] ))
					$idlanguage = $params['idlanguage'];
			}
		}
		$result = array();
		$db		= wcCore::getDatabaseHandle();
		$items = $db->querySelect('News', sprintf( "idLanguage = '%s' and attrStatus = 1 order by dateAdded desc", $idlanguage ), "DATE_FORMAT(dateAdded, '%Y-%m-%d %H:%i') timestamp, content" ,  $limit);  
		return $items;
	}
	static function addNews($params=array())
	{
		$idLanguage	= 'en';
		$content 	= '';
		
		if (isset( $params) )
		{
			if (is_array( $params ))
			{
				if ( isset( $params['content'] ))
					$content = $params['content'];
	
				if ( isset( $params['idLanguage'] ))
					$idlanguage = $params['idLanguage'];
			}
		}
		if (strlen($content)>0)
			if (strlen($idLanguage))
			{
				$db		= wcCore::getDatabaseHandle();
				return $db->queryInsert('News', 'idLanguage, content' ,  sprintf( "'%s','%s'", $idlanguage, addslashes($content) ) );  
			}
		return 0;
	}
	
	static function updateNewsContent($id, $content)
	{
		$db		= wcCore::getDatabaseHandle();
		$content= addslashes( $content );

		return $db->queryUpdate('News', sprintf("content = '$content'") , sprintf( "idNews = %d",  $id )  )  ;  
	}
}
?>